#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStackedWidget>
#include <QDebug>
#include <QDesktopServices>
#include <QUrl>
#include <QStyle>
#include <QFile>
#include <QTemporaryFile>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);

    // 创建 TCcalculator 和 SCcalculator 和 Graphcalculator实例
    tcCalculator = new TCcalculator(this); // 确保传递 this 作为父对象
    scCalculator = new SCcalculator(this); // 同上
    graphCalculator = new Graphcalculator(this);  // 假设 GraphCalculatorWidget 是您的 widget 类名

    // 将计算器添加到 QStackedWidget 中
    ui->stackedWidget->addWidget(scCalculator); // 添加 SCcalculator
    ui->stackedWidget->addWidget(tcCalculator); // 添加 TCcalculator
    ui->stackedWidget->addWidget(graphCalculator);// 添加 Graphcalculator

    // 连接信号和槽
    connect(ui->actionTranslationCalculator, &QAction::triggered, [this]() {
        ui->stackedWidget->setCurrentWidget(tcCalculator);
    });

    connect(ui->pushButtonTranslationCalculator, &QPushButton::clicked, [this]() {
        ui->stackedWidget->setCurrentWidget(tcCalculator);
    });
    connect(ui->actionScientificCalculator, &QAction::triggered, [this]() {
        ui->stackedWidget->setCurrentWidget(scCalculator);
    });
    connect(ui->pushButtonScientificCalculator, &QPushButton::clicked, [this]() {
        ui->stackedWidget->setCurrentWidget(scCalculator);
    });

    // 说明文件
    connect(ui->pushButtonInstruction, &QPushButton::clicked, this, &MainWindow::on_pushButtonInstruction_clicked);

    connect(ui->actionStar, &QAction::triggered, this, &MainWindow::on_actionStar_triggered);//星空背景
    connect(ui->actionGrass, &QAction::triggered, this, &MainWindow::on_actionGrass_triggered);//草原背景
    connect(ui->actionYourName, &QAction::triggered, this, &MainWindow::on_actionYourName_triggered);//你的名字背景
    connect(ui->actionDefault, &QAction::triggered, this, &MainWindow::on_actionDefault_triggered);//无背景


}
MainWindow::~MainWindow()
{
    delete tcCalculator; // TCcalculator 实例将被删除
    delete scCalculator; // SCcalculator 实例将被删除
    delete ui; // UI 资源将被清理
}
//使用说明
void MainWindow::on_pushButtonInstruction_clicked() {
    // 定义文件的完整路径
    QString filePath = "D:/CppHomework/MagicCalculator/source/instruction.pdf";
    // 使用 QDesktopServices 打开文件
    QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}

void MainWindow::on_actionTranslationCalculator_triggered() {
    ui->stackedWidget->setCurrentWidget(tcCalculator);
    ui->stackedWidget->resize(567,525);
}

void MainWindow::on_pushButtonTranslationCalculator_clicked() {
    ui->stackedWidget->setCurrentWidget(tcCalculator);
    ui->stackedWidget->resize(567,525);
}

void MainWindow::on_actionScientificCalculator_triggered() {
    ui->stackedWidget->setCurrentWidget(scCalculator);
   // ui->stackedWidget->resize(567,525);
}

void MainWindow::on_pushButtonScientificCalculator_clicked() {
    ui->stackedWidget->setCurrentWidget(scCalculator);
   // ui->stackedWidget->resize(567,525);
}

void MainWindow::on_actionGraphCalculator_triggered() {
    ui->stackedWidget->setCurrentWidget(graphCalculator);
    ui->stackedWidget->resize(900,750);
}

void MainWindow::on_pushButtonGraphCalculator_clicked() {
    ui->stackedWidget->setCurrentWidget(graphCalculator);
    ui->stackedWidget->resize(900,750);
}

void MainWindow::on_actionStar_triggered() {
    // 设置背景图片
    QPixmap background(":/star.jpg");
    background = background.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 根据窗口大小调整图片大小
    QPalette palette;
    palette.setBrush(QPalette::Background, background);
    this->setPalette(palette);
    ui->labelWelcome->setStyleSheet("QLabel { color : white; }");
}
void MainWindow::on_actionGrass_triggered() {
    // 设置背景图片
    QPixmap background(":/grass.jpg");
    background = background.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 根据窗口大小调整图片大小
    QPalette palette;
    palette.setBrush(QPalette::Background, background);
    this->setPalette(palette);
    ui->labelWelcome->setStyleSheet("QLabel { color : white; }");
}
 void MainWindow::on_actionYourName_triggered()
 {
     // 设置背景图片
     QPixmap background(":/yourname.jpg");
     background = background.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 根据窗口大小调整图片大小
     QPalette palette;
     palette.setBrush(QPalette::Background, background);
     this->setPalette(palette);
     // 设置labelWelcome的字体颜色为白色
     ui->labelWelcome->setStyleSheet("QLabel { color : white; }");
 }
void MainWindow::on_actionDefault_triggered() {
    QPalette palette = this->style()->standardPalette(); // 获取默认的调色板
    this->setPalette(palette);
    ui->labelWelcome->setStyleSheet("QLabel { color : black; }");
}
